package com.xinzhi.service.impl;

import com.xinzhi.service.IVerifyService;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import com.xinzhi.utils.TengXunSMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VerifyServiceImpl implements IVerifyService {
    @Autowired
    private TengXunSMSUtils tengXunSMSUtils;

    @Autowired
    private RedisUtils redisUtils;
    @Override
    public Result getCode(String phone) {
        if (phone== null)
            return Result.error("手机号不能为空");
        if (!isValidPhoneNumber(phone))
            return Result.error("手机号格式不对");
        //生成一个6位数的验证码
        Integer authCode =tengXunSMSUtils.generateValidateCode(6);
        String strAuthCode = authCode.toString();
        tengXunSMSUtils.sendShortMessage("2193808", phone, strAuthCode, "1");
        redisUtils.set(phone,strAuthCode,60);
        return Result.ok("验证码发送成功");
    }

    @Override
    public boolean isValidAuthCode(String authCode) {return authCode.matches("^\\d{6}$");}

    @Override
    public Result iVAuthCode(String authCode,String phone) {
        if (redisUtils.get(phone)==null)
            return Result.error("验证码已过期");
        if (!redisUtils.get(phone).equals(authCode))
            return Result.error("验证码错误");
        return Result.ok("验证码正确");
    }

    @Override
    public boolean isValidPhoneNumber(String phone) {
        return phone.matches("^1[3456789]\\d{9}$");
    }

    @Override
    public boolean isValidPassword(String password) {
        return password.matches("^[a-zA-Z0-9]{8,30}$");
    }


    @Override
    public boolean isValidIdCode(String idCode) {
        return idCode.matches("^\\d{17}[\\d|x|X]$");
    }

    @Override
    public boolean isCard_Number(String card_number) {return card_number.matches("^(?:622202|622203|622848|621661|621700|622588)\\d{10,13}$");}
}
